/** добавить обновление страниц по таймеру
 *
 * @author ilya portnyagin iportnyagin@gmail.com
 */

package ru.portnyagin.helpdeskru.controller.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedProperty;
import ru.portnyagin.helpdeskru.controller.CurrentRequestBean;
import ru.portnyagin.helpdeskru.controller.LoginController;
import ru.portnyagin.helpdeskru.model.Request;
import ru.portnyagin.helpdeskru.model.StateRequest;
import ru.portnyagin.helpdeskru.model.UserHD;
import ru.portnyagin.helpdeskru.service.RequestService;

public class ServiceController implements Serializable {

    @EJB
    private ru.portnyagin.helpdeskru.service.RequestService requestService;

    @ManagedProperty("#{loginController}")
    private LoginController loginController;
    
    @ManagedProperty("#{currentRequestBean}")
    private CurrentRequestBean currentRequestBean;
    
    private List<Request> listRequest;
    private UserHD curUser;
    protected String actionString;
    protected List<StateRequest> listStateRequest;
    protected boolean showRequestOnlyForCurrentUser = true;

    public ServiceController() {
    }
    
    @PostConstruct
    private void postConstruct() {
        curUser = loginController.getLogged();
        currentRequestBean.setActionString(actionString);
    }


    public List<Request> getListRequest() {
        if (listRequest == null) {
            listRequest = new ArrayList<>();
            listRequest = getRequestService().findByStateAndPerfomer(listStateRequest, curUser, showRequestOnlyForCurrentUser);
        }
        return listRequest;
    }

    
    public void setLoginController(LoginController loginController) {
        this.loginController = loginController;
    }

    public void setCurrentRequestBean(CurrentRequestBean currentRequestBean) {
        this.currentRequestBean = currentRequestBean;
    }

    private RequestService getRequestService() {
        return requestService;
    }

    public boolean isShowRequestOnlyForCurrentUser() {
        return showRequestOnlyForCurrentUser;
    }

    public void setShowRequestOnlyForCurrentUser(boolean showRequestOnlyForCurrentUser) {
        this.showRequestOnlyForCurrentUser = showRequestOnlyForCurrentUser;
    }

    public void showRequestChangeListener() {
        listRequest = null;
    }
}
